package com.sogoodsoft.web.common;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;
import org.springside.modules.utils.encode.JsonBinder;
import org.springside.modules.utils.web.struts2.Struts2Utils;

import com.sogoodsoft.entity.app.SysClass;
import com.sogoodsoft.service.app.SysClassService;
import com.sogoodsoft.web.CrudActionSupport;

@Results({ @Result(name = "SYSCLASS", location = "/WEB-INF/content/common/dialogType.jsp"),
        @Result(name = "SYSCLASSEdit", location = "/WEB-INF/content/sys/sys-type.jsp") })
public class SysTypeUIAction extends CrudActionSupport < SysClass > {

    private static final long serialVersionUID = -4052047494894591406L;

    private static JsonBinder binder = JsonBinder.buildNonDefaultBinder();

    private SysClassService sysClassSer;

    private Long id;

    private SysClass sysClass;

    private List < SysClass > tree;

    private String code;

    private String ul;

    public String getTypeByCode() {
        logger.debug("getTypeByCode");
        Map < String, Object > msg = new HashMap < String, Object >();
        if (code != null) {
            sysClass = sysClassSer.getByCode(code);
        }

        if (sysClass == null) {
            msg.put("error", "true");
            msg.put("msg", "未找到对应类型！");
            return null;
        } else {
            msg.put("typeName", sysClass.getName());
            msg.put("id", sysClass.getId());
            tree = sysClass.getChild();
            removeCascade(tree);
            msg.put("error", "false");
            msg.put("type", tree);
            String json = binder.toJson(msg);
            logger.debug(json);
            Struts2Utils.renderJson(json, "encoding:utf-8");
        }

        return null;
    }

    /**
     * 递归按级别生成UL
     * 
     * @param org
     * @return
     */
    public String ulComposite(SysClass cls) {
        StringBuffer ul = new StringBuffer();
        if (cls.hasChild()) {
            ul.append("<li><a href=\"#\" parentId=\"" + cls.getParentId() + "\" id=\"" + cls.getId()
                    + "\" class=\"typeTree\" >" + cls.getName() + "</a>\n");
            ul.append("<ul>\n");
            List < SysClass > list = cls.getChild();
            for (SysClass sc : list) {
                ul.append(ulComposite(sc));
            }
            ul.append("</ul>\n");
            ul.append("</li>\n");
        } else {
            ul.append("<li><a href=\"#\" parentId=\"" + cls.getParentId() + "\" id=\"" + cls.getId()
                    + "\" class=\"typeLeaf\">" + cls.getName() + "</a></li>\n");
        }
        return ul.toString();
    }

    public String getSysClassUL() {
        if (code == null) {
            logger.error("组织结构获取错误，没有传递起始节点！");
        }
        sysClass = sysClassSer.getByCode(code);
        ul = ulComposite(sysClass);
        return "SYSCLASS";
    }

    public String getAllSysClassUL() {
        sysClass = sysClassSer.getFirst();
        ul = ulComposite(sysClass);
        return "SYSCLASSEdit";
    }

    public void removeCascade(List < SysClass > list) {
        int length = list.size();
        for (int i = 0; i < length; i++) {
            SysClass sy = list.get(i);
            sy.setParent(null);
            sy.setChild(null);
            list.set(i, sy);
        }
    }

    @Override
    public String delete() throws Exception {
        return null;
    }

    @Override
    public String input() throws Exception {
        return null;
    }

    @Override
    public String list() throws Exception {
        return null;
    }

    @Override
    protected void prepareModel() throws Exception {

    }

    @Override
    public String save() throws Exception {
        return null;
    }

    @Override
    public SysClass getModel() {
        return null;
    }

    public SysClassService getSysClassSer() {
        return sysClassSer;
    }

    @Autowired
    public void setSysClassSer(SysClassService sysClassSer) {
        this.sysClassSer = sysClassSer;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public SysClass getSysClass() {
        return sysClass;
    }

    public void setSysClass(SysClass sysClass) {
        this.sysClass = sysClass;
    }

    public List < SysClass > getTree() {
        return tree;
    }

    public void setTree(List < SysClass > tree) {
        this.tree = tree;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getUl() {
        return ul;
    }

    public void setUl(String ul) {
        this.ul = ul;
    }

}
